/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.purpose.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.swan.bean.MessageBean;

import com.koron.common.bean.StaffBean;
import com.koron.common.domain.DataBean;
import com.koron.common.generator.DefaultIdGenerator;
import com.koron.purpose.domain.SafetyOrgStaff;
import com.koron.purpose.mapper.SafetyOrgStaffMapper;
import com.koron.purpose.service.api.SafetyOrgStaffService;

@Service
public class SafetyOrgStaffServiceImpl implements SafetyOrgStaffService {

	@SuppressWarnings({ "rawtypes", "deprecation" })
	@Override
	public MessageBean<?> insert(SafetyOrgStaff bean, StaffBean user) {
		MessageBean<?> msg = MessageBean.create(0, "success", void.class);
		Map map = verification(bean);
		int code = (int) map.get("code");
        String description = (String) map.get("description");
        if(code != 0){
        	msg.setCode(code);
        	msg.setDescription(description);
        	return msg;
        }
        SessionFactory factory = new SessionFactory();
		try {
			SafetyOrgStaffMapper mapper = factory.getMapper(SafetyOrgStaffMapper.class);
			/*code = mapper.addVerification(bean.getStaffCode(), bean.getOrgId());
			if(code > 0){
				factory.close(false);
				msg.setCode(35001);
	        	msg.setDescription("人员已存在");
	        	return msg;
			}*/
			bean.setAddUser(user.getCode());
			bean.setAddUserName(user.getName());
			bean.setAddTime(new Date());
			bean.setUpdateUser(user.getCode());
			bean.setUpdateUserName(user.getName());
			bean.setUpdateTime(new Date());
			bean.setId(DefaultIdGenerator.getInstance().generateLongId());
			mapper.insert(bean);
			factory.close();
		} catch (Exception e) {
			factory.close(false);
			msg.setCode(35001);
			msg.setDescription("数据库连接异常");
		}
		return msg;
	}

	@SuppressWarnings({ "deprecation", "rawtypes" })
	@Override
	public MessageBean<?> update(SafetyOrgStaff bean, StaffBean user) {
		MessageBean<?> msg = MessageBean.create(0, "success", void.class);
		Map map = verification(bean);
		int code = (int) map.get("code");
        String description = (String) map.get("description");
        if(code != 0){
        	msg.setCode(code);
        	msg.setDescription(description);
        	return msg;
        }
		if(StringUtils.isEmpty(bean.getId())){
			msg.setCode(35001);
        	msg.setDescription("参数校验异常");
        	return msg;
		}
		SessionFactory factory = new SessionFactory();
		try {
			SafetyOrgStaffMapper mapper = factory.getMapper(SafetyOrgStaffMapper.class);
			/*code = mapper.updateVerification(bean.getStaffCode(), bean.getOrgId(), bean.getId());
			if(code > 0){
				factory.close(false);
				msg.setCode(35001);
	        	msg.setDescription("人员已存在");
	        	return msg;
			}*/
			bean.setUpdateUser(user.getCode());
			bean.setUpdateUserName(user.getName());
			bean.setUpdateTime(new Date());
			
			mapper.update(bean);
			factory.close();
		} catch (Exception e) {
			factory.close(false);
			msg.setCode(35001);
			msg.setDescription("数据库连接异常");
		}
		return msg;
	}

	@SuppressWarnings("deprecation")
	@Override
	public MessageBean<?> delete(String id) {
		MessageBean<?> msg = MessageBean.create(0, "success", void.class);
		try(SessionFactory factory = new SessionFactory();){
			factory.getMapper(SafetyOrgStaffMapper.class).delete(id);
		} catch (Exception e) {
			msg.setCode(35001);
			msg.setDescription("数据库连接异常");
		}
		return msg;
	}

	@SuppressWarnings({ "rawtypes", "deprecation", "unchecked" })
	@Override
	public MessageBean<?> list(String name,String orgId,Integer page,Integer pageSize,String keyword) {
		MessageBean<DataBean> msg= MessageBean.create(0, "success", DataBean.class);
		Integer totalPage = null;
	    Integer totalNumber = null;
	    Integer start = null;
	    Integer end = null;
	    if (null!=page && pageSize!=null){
            if (page >0 && pageSize > 0){
            }else{
            	//第一页 10条数据
            	page =1;
            	pageSize=10;
            }
            start=(page<=1)?0:(page-1)*pageSize;
            end = pageSize;
        }
	    if(name == null){
	    	name = "%%";
	    }else{
	    	name = "%"+name+"%";
	    }
	    try(SessionFactory factory = new SessionFactory();){
	    	SafetyOrgStaffMapper mapper = factory.getMapper(SafetyOrgStaffMapper.class);
	    	totalNumber = mapper.count(name,orgId,keyword);
	    	if(start != null && end != null){
				totalPage = (totalNumber+pageSize-1)/pageSize;
			}else{
				totalPage = 1;
				page = totalPage;
				pageSize = totalNumber;
			}
	    	DataBean data = new DataBean();
			data.setList(mapper.query(name, orgId, start, end,keyword));
			data.setPageNo(page);
			data.setPageSize(pageSize);
			data.setTotalNumber(totalNumber);
			data.setTotalPage(totalPage);
			msg.setData(data);
		} catch (Exception e) {
			msg.setCode(35001);
			msg.setDescription("数据库连接异常");
		}
		return msg;
	}

	@SuppressWarnings("deprecation")
	@Override
	public MessageBean<?> queryById(String id) {
		MessageBean<SafetyOrgStaff> msg = MessageBean.create(0, "success", SafetyOrgStaff.class);
		SafetyOrgStaff bean = new SafetyOrgStaff();
		try(SessionFactory factory = new SessionFactory();){
			bean = factory.getMapper(SafetyOrgStaffMapper.class).queryById(id);
			msg.setData(bean);
		} catch (Exception e) {
			msg.setCode(35001);
			msg.setDescription("数据库连接异常");
		}
		return msg;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private Map verification(SafetyOrgStaff bean){
		Map map = new HashMap();
		if(bean == null){
			map.put("code", 35002);
    		map.put("description", "参数校验异常");
    		return map;
		}
		if(StringUtils.isEmpty(bean.getStaffCode()) || StringUtils.isEmpty(bean.getStaffName())){
			map.put("code", 35002);
    		map.put("description", "人员不能为空");
    		return map;
		}
		if(StringUtils.isEmpty(bean.getDeptCode()) || StringUtils.isEmpty(bean.getDeptName())){
			map.put("code", 35002);
    		map.put("description", "部门不能为空");
    		return map;
		}
		if(StringUtils.isEmpty(bean.getOrgRole())){
			map.put("code", 35002);
    		map.put("description", "角色不能为空");
    		return map;
		}
		if(StringUtils.isEmpty(bean.getNote())){
			map.put("code", 35002);
    		map.put("description", "职责描述不能为空");
    		return map;
		}
		if(StringUtils.isEmpty(bean.getOrgId())){
			map.put("code", 35002);
    		map.put("description", "机构不能为空");
    		return map;
		}
		if(bean.getSerial() == null){
			map.put("code", 35002);
    		map.put("description", "排序号不能为空");
    		return map;
		}
		if(bean.getIsleader() == null){
			bean.setIsleader(0);
		}
		map.put("code", 0);
		map.put("description", "校验成功");
		return map;
	}
}
